﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OptimumInfra.Models;
using OptimumInfra.ViewData;
using System.Collections.Specialized;

namespace OptimumInfra.Controllers
{
    public class ResourceController : Controller
    {
        //
        // GET: /Resource/

        private ResourceRepository repo = new ResourceRepository();

        public ActionResult Index()
        {

            string message = Request.QueryString["step"];
            if (message != null || message == "")
                ViewData["message"] = "Step 2/3: Choose your server resources";
            ViewData["resources"] = this.repo.GetAllResource();
            return View(ViewData);

        }

        //
        // GET: /Resource/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Resource/Create

        public ActionResult Create()
        {
            Resource resource = new Resource();
            return View(resource);
        } 

        //
        // POST: /Resource/Create

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Resource resource = new Resource();
                UpdateModel(resource);
                resource =  repo.CreateResource(resource);
                TempData["info"] = "Create resource successfully";
                return RedirectToAction("Edit", "Resource", new { id = resource.Id });
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Resource/Edit/5
          [Authorize(Roles = "admin")]
        public ActionResult Edit(int id)
        {
            ViewDataResource resource = new  ViewDataResource();
            resource = this.repo.GetViewDataResourceById(id);
            return View(resource);
        }

        //
        // POST: /Resource/Edit/5

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                
                ViewDataResource resource = new ViewDataResource();
                UpdateModel(resource);
                Resource r = new Resource();
                r.Id = resource.Id;
                r.Name = resource.Name;
                r.Description = resource.Description;
                r.Unit = resource.Unit;
      
                this.repo.UpdateResource(r);
                TempData["info"] = "Update resource successfully";
                return RedirectToAction("Edit", "Resource", new { id = id });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Resource/Delete/5
          [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {

            Resource resource = this.repo.GetResourceById(id);
            this.repo.DeleteResourceById(resource);
            return RedirectToAction("Manage");
        }
        [Authorize(Roles = "admin")]
        public ActionResult DeleteAttribute(int id)
        {
            string a = Request.QueryString["amount"];
            int amount = 0;
            try
            {
                amount = Int32.Parse(a);
            }
            catch (Exception e)
            {
                return RedirectToAction("Edit", "Resource", new { id = id });
            }
            if(amount == 0)
                return RedirectToAction("Edit", "Resource", new { id = id });
            OptimumInfra.Models.Attribute attribute = this.repo.GetAttributeByAmount(amount, id);
            this.repo.DeleteAttributeById(attribute);
            TempData["info"] = "Delete attribute successfully";
            return RedirectToAction("Edit", "Resource", new { id = id });
        }
        [Authorize(Roles = "admin")]
        public ActionResult Manage()
        {
            ViewData["resources"] = this.repo.GetAllResource();
            return View(ViewData);
        }

        //For tst

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult SavePrice(int id)
        {

            NameValueCollection nvc = Request.Form;
            int amount = 0, price = 0;
            if (!string.IsNullOrEmpty(nvc["new_amount"]))
            {
                amount = Int32.Parse(nvc["new_amount"]);
            }

            if (!string.IsNullOrEmpty(nvc["new_price"]))
            {
                price = Int32.Parse(nvc["new_price"]);
            }
            if (amount != 0 && price != 0)
            {
                this.repo.CreateResourceAttribute(id, amount, price);
            }
            TempData["info"] = "Create new value successfully";
            return RedirectToAction("Edit", "Resource", new { id = id });

        }
        public ActionResult UpdateAttribute(int id)
        {

            NameValueCollection nvc = Request.Form;
            int amount = 0, price = 0;
            if (!string.IsNullOrEmpty(nvc["new_amount"]))
            {
                amount = Int32.Parse(nvc["new_amount"]);
            }

            if (!string.IsNullOrEmpty(nvc["new_price"]))
            {
                price = Int32.Parse(nvc["new_price"]);
            }
            if (amount != 0 && price != 0)
            {
                this.repo.CreateResourceAttribute(id, amount, price);
            }
            TempData["info"] = "Update uccessfully";
            return RedirectToAction("Edit", "Resource", new { id = id });

        }
        [HttpPost]
        [Authorize(Roles = "admin")]

        public string SavePrice_backup(string Id, string new_amount, string new_price)
        {
            OptimumInfra.Models.Attribute a;
            try
            {
                int id = Int32.Parse(Id);
                int amount = Int32.Parse(new_amount);
                int price = Int32.Parse(new_price);
                a = this.repo.CreateResourceAttribute(id, amount, price);
            }
            catch (Exception e)
            {
                return "<td colspan='4'>Invalid input value</td>";
            }
            /*
          string test = "<tr>
          <td><label for='Id'>Id</label></td>
          <td>          
               <div class='editor-field'>
                <input type='text' value='"+new_amount+"' name='amount'>
               </div>
            </td> 
            <td>
                <div class='editor-field'>
                <input type='text' value='"+new_price+"' name='price'>
               </div>
           </td> 
           <td>
               <a href='/Resource/deleteattribute/7?amount=10' onclick='return confirm('Delete attribute?');'>Delete</a>

           </td>
          <tr id="textEntered">
           </tr> ";
             * */
            return "<td><label for='Id'>Id</label></td><td>"+new_amount+"</td><td>"+new_price +"</td><td><a href='/resource/deleteattribute/"+Id+"?amount="+a.Id+"'>Delete</a>";
           
        }

    }
}
